<?php
/*************************************************************************************
 * exportkml.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.9 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/14 14:32:43 $
 *
 * CHANGES
 * -------
 * 2006/04/30 (0.5)
 * - internationalization : fr en
 *
 * 2006/03/06 (0.3)
 *  -  First Release
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/

$showtrip = urldecode($_GET["showtrip"]);

include_once("dbaccess.php");

$tripName = "All";
$triparray = array();
if (!empty($showtrip) && $showtrip != "All") {
	$tripName = $showtrip;
	$triparray[$tripName] = 1;
} else {
	$triparray = getTripSummary();
}

$fpw = fopen($showtrip.".kml", "w");
fwrite($fpw, "<"."?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fwrite($fpw, '<kml xmlns="http://earth.google.com/kml/2.0"> ');
fwrite($fpw, "\r\n");
fwrite($fpw, '<Document>');
fwrite($fpw, "\r\n");
fwrite($fpw, "<name>".$tripName.".kml</name>");
fwrite($fpw, "\r\n");

// all trip points
foreach ($triparray as $trip => $tripnb) {
	$tag = getTripPoints($trip);
	ksort($tag);
	reset($tag);
	printTrip($trip, $tag);
}

function printTrip($name, $tag) {
	global $fpw;
	fwrite($fpw, "<Folder>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, "<name>".$name."</name>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, "<open>1</open>");
	fwrite($fpw, "\r\n");
	foreach($tag as $book) {
		printPoint($book["name"], $book["lat"],$book["long"],$book["url"],$book["photos"], $book["trips"], $book);
	}
	fwrite($fpw, ' </Folder> ');
}
function printPoint($destiName, $destiLat, $destiLng, $destiUrl, $destiPhotos, $trips, $destiArray) {
	global $fpw, $moreinfoNames;

	fwrite($fpw, "<Placemark>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, "<name>".$destiName."</name>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<description> <![CDATA[<table><tr><td>');
	if (!empty($destiUrl)) {
		fwrite($fpw, "<a href=\"".$destiUrl."\">".$destiName."</a>");
		fwrite($fpw, "<br/>");
	}
	fwrite($fpw, "Lat:".$destiLat);
	fwrite($fpw, "<br/>");
	fwrite($fpw, "Lng:".$destiLng);
	fwrite($fpw, "<br/>");
	fwrite($fpw, "Trips:".$trips);
	fwrite($fpw, "<br/>");
	foreach($moreinfoNames as $name) {
		if (!empty($destiArray[$name])) {
			if ($name == "referer") {
				fwrite($fpw, " ".$name.":<a href=\"".$destiArray[$name]."\">".$destiArray[$name]."</a><br/>");
			} else {
				fwrite($fpw, " ".$name.":".$destiArray[$name]." <br/>");
			}
		}
	}
	fwrite($fpw, "</td><td>");
	foreach ($destiPhotos as $photo) {
		$photourl = $photo['url'];
		if (!empty($photourl)) {
			fwrite($fpw, "<a href='".$photourl."'><img src='".$photourl."' /></a>");
			fwrite($fpw, "<br/>");
		}
	}   
	fwrite($fpw, '</td></tr></table>]]> </description>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<LookAt>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, "<longitude>".$destiLng."</longitude>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, "<latitude>".$destiLat."</latitude>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<altitude>0</altitude>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<range>1380.767325838837</range>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<tilt>1.202861478787585e-10</tilt>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<heading>-0.04546393204637901</heading>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '</LookAt>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<styleUrl>root://styles#default+icon=0x307</styleUrl>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '<Point>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, "<coordinates>".$destiLng.",".$destiLat.",0</coordinates>");
	fwrite($fpw, "\r\n");
	fwrite($fpw, '</Point>');
	fwrite($fpw, "\r\n");
	fwrite($fpw, '</Placemark>');
	fwrite($fpw, "\r\n");
}

fwrite($fpw, ' </Document> </kml>');
fclose($fpw);  

echo '<a href="'.$showtrip.'.kml">'.$lang_download_kml.' '.$showtrip.'</a>';
?>
